import { notification } from "antd";
import axios from "axios";
import { navigateTo } from "./navigate";
import { baseUrl } from "./env";
import auth from "./auth";
const request = axios.create({
  baseURL: baseUrl
})

request.interceptors.request.use((req: any) => {
  req.headers['customertoken'] = auth.getToken()
  return req
})
request.interceptors.response.use((res: any) => {
  if(res.data instanceof Blob ){
    return res.data
  }
  else if (res.data.code != 200) {
    
    if (['SYS50001'].includes(res.data.code)) {
      auth.toLogin()
    }
    
    notification.error({
      message: '接口提示',
      description: res.data.msg||'系统错误'
    })
    return Promise.reject(res.data)
  } else {
    return res.data.data
  }
  return res
}, (err: { config: { url: any; }; message: any; }) => {
  console.error(err)
  notification.error({
    message: '接口提示',
    description: `接口${err.config.url}:${err.message}`
  })
})

export default request